﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using ERPStudio;

namespace ERPForms
{
    public partial class MesajeFrm : FrmCard
    {

        private Boolean _adauga = false;
        public Boolean AdaugaMesaj
        {
            get { return _adauga; }
            set { _adauga = value; }
        }

        private int _idMesaj = -1;
        public int IDMesaj
        {
            get { return _idMesaj; }
            set { _idMesaj = value; }
        }
        public ListViewItem _Item = null;
        private Guid _UserId = Guid.Empty;
        public MesajeFrm(Guid userID,ListViewItem item,ModuleClass mc,Boolean _addMsg,int _idm):base(mc)
        {
            _Item = item;
            _UserId = userID;
            InitializeComponent();
            workConnString = "ESRPConnectionString";
            eSRPDataSet.Mesaje.UserExpColumn.DefaultValue = _UserId;
            eSRPDataSet.Mesaje.DataExpColumn.DefaultValue = DateTime.Now;
            AdaugaMesaj = _addMsg;
            IDMesaj = _idm;
        }

        private void FontToolStripButton_Click(object sender, EventArgs e)
        {
            if(fontDialog.ShowDialog() != DialogResult.Cancel)
                rtb.SelectionFont = fontDialog.Font;
        
        }

        private void FontColorToolStripButton_Click(object sender, EventArgs e)
        {
            if (ColorDlg.ShowDialog() != DialogResult.Cancel)
                rtb.SelectionColor = ColorDlg.Color;
        
        }

        private void BoldToolStripButton_Click(object sender, EventArgs e)
        {
            //Switch Bold
            System.Drawing.Font  currentFont = rtb.SelectionFont;
            System.Drawing.FontStyle newFontStyle =new FontStyle();
            if(rtb.SelectionFont.Bold == true)
                newFontStyle = (FontStyle)(currentFont.Style - FontStyle.Bold);
            else
                newFontStyle = (FontStyle)((int)currentFont.Style + (int)FontStyle.Bold);
            
            rtb.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size, newFontStyle);

            // Check/Uncheck Bold button
            BoldToolStripButton.Checked = rtb.SelectionFont.Bold;
        }

        private void UnderlineToolStripButton_Click(object sender, EventArgs e)
        {
            System.Drawing.Font  currentFont = rtb.SelectionFont;
            System.Drawing.FontStyle newFontStyle =new FontStyle();
            if(rtb.SelectionFont.Underline)
                newFontStyle = (FontStyle)(currentFont.Style - FontStyle.Underline);
            else
                newFontStyle = (FontStyle)((int)currentFont.Style + (int)FontStyle.Underline);
            
            rtb.SelectionFont = new Font(currentFont.FontFamily, currentFont.Size, newFontStyle);

            UnderlineToolStripButton.Checked = rtb.SelectionFont.Underline;
        }

        private void LeftToolStripButton_Click(object sender, EventArgs e)
        {
            rtb.SelectionAlignment = HorizontalAlignment.Left;
        }

        private void CenterToolStripButton_Click(object sender, EventArgs e)
        {
            rtb.SelectionAlignment = HorizontalAlignment.Center;
        }

        private void RightToolStripButton_Click(object sender, EventArgs e)
        {
            rtb.SelectionAlignment = HorizontalAlignment.Right;
        }

        private void BulletsToolStripButton_Click(object sender, EventArgs e)
        {
            rtb.SelectionBullet = !rtb.SelectionBullet;
            BulletsToolStripButton.Checked = rtb.SelectionBullet;
        }

        private void MesajeFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con1 = DeschideConexiune("AdminConnectionString");
            SqlConnection con2 = DeschideConexiune(workConnString);
            this.mesajeTableAdapter.Connection = con2;
            this.mesajeTableAdapter.Fill(this.eSRPDataSet.Mesaje,_UserId);
            this.aspnet_UsersTableAdapter.Connection = con1;
            this.aspnet_UsersTableAdapter.Fill(this.eSRPDataSet.aspnet_Users);
            this.KeyPreview = false;
            InchideConexiune(con1);
            InchideConexiune(con2);

            AddBindingSource(mesajeBindingSource);
            this.listTableAdaptor.Add(mesajeTableAdapter.Adapter);

            /// functia care se apeleaza in momentul in care se apasa F9
            HandleSalvare += new EventHandler(Trimite);
            if (_adauga)
                Adauga(this, null);
            if (_idMesaj != -1)
            {
                mesajeBindingSource.Filter = "IDMesaj=" + _idMesaj.ToString();
                splitContainer1.Panel1.Enabled = false;
                // rtb.ReadOnly = true;
                this.ReadOnly = true;
            }
            else
            {
                //filtreaza sa nu se afiseze celelalte mesaje
                mesajeBindingSource.Filter = "IDMesaj=-1";
                AddButton("Trimite", global::ERPStudio.Properties.Resources._001_51, Trimite, ToolStripItemAlignment.Right, true);
            }
        }

        private void Trimite(object sender, EventArgs e)
        {
            base.Salvare(this, null);
            if (EroareSalvare)
                return;
            this.Close();
        }

        private void MesajeFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_Item != null)
                _Item.SubItems[3].Text = "on";
        }
    }
}
